package com.tonann.app.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.tonann.app.domain.AppSensor;
import com.tonann.app.service.IAppDeviceService;
import com.tonann.app.service.IAppSensorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tonann.common.annotation.Log;
import com.tonann.common.core.controller.BaseController;
import com.tonann.common.core.domain.AjaxResult;
import com.tonann.common.enums.BusinessType;
import com.tonann.app.domain.AppFault;
import com.tonann.app.service.IAppFaultService;
import com.tonann.common.utils.poi.ExcelUtil;
import com.tonann.common.core.page.TableDataInfo;

/**
 * 故障记录Controller
 *
 * @author tonann
 * @date 2022-10-15
 */
@RestController
@RequestMapping("/app/fault")
public class AppFaultController extends BaseController
{
    @Resource
    private IAppFaultService appFaultService;
    @Resource
    private IAppSensorService sensorService;

    @Resource
    private IAppDeviceService deviceService;

    /**
     * 查询故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('app:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppFault appFault)
    {
        startPage();
        List<AppFault> list = appFaultService.selectAppFaultList(appFault);
        for(int i = 0 ; i < list.size();i++){
            AppFault fault = list.get(i);
            if (fault.getSensorId() !=null){
                AppSensor appSensor = sensorService.selectAppSensorBySensorId(fault.getSensorId());
                if (appSensor != null){
                    fault.setSensor(appSensor);
                }
                fault.setDevice(deviceService.selectAppDeviceByDeviceId(fault.getDeviceId()));
            }
            list.set(i,fault);
        }
        return getDataTable(list);
    }

    /**
     * 导出故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('app:fault:export')")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppFault appFault)
    {
        List<AppFault> list = appFaultService.selectAppFaultList(appFault);
        ExcelUtil<AppFault> util = new ExcelUtil<AppFault>(AppFault.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:fault:query')")
    @GetMapping(value = "/{faultId}")
    public AjaxResult getInfo(@PathVariable("faultId") Long faultId)
    {
        return AjaxResult.success(appFaultService.selectAppFaultByFaultId(faultId));
    }

    /**
     * 新增故障记录
     */
    @PreAuthorize("@ss.hasPermi('app:fault:add')")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppFault appFault)
    {
        return toAjax(appFaultService.insertAppFault(appFault));
    }

    /**
     * 修改故障记录
     */
    @PreAuthorize("@ss.hasPermi('app:fault:edit')")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppFault appFault)
    {
        return toAjax(appFaultService.updateAppFault(appFault));
    }

    /**
     * 删除故障记录
     */
    @PreAuthorize("@ss.hasPermi('app:fault:remove')")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faultIds}")
    public AjaxResult remove(@PathVariable Long[] faultIds)
    {
        return toAjax(appFaultService.deleteAppFaultByFaultIds(faultIds));
    }
}
